package com.dlc.shop.user.common.listener;

import com.dlc.shop.bean.enums.ScoreLogType;
import com.dlc.shop.bean.event.LevelUpEvent;
import com.dlc.shop.bean.model.UserExtension;
import com.dlc.shop.bean.param.ScoreConfigParam;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.util.DataBatchHandleUtil;
import com.dlc.shop.dao.UserExtensionMapper;
import com.dlc.shop.service.SysConfigService;
import com.dlc.shop.user.common.model.UserLevel;
import com.dlc.shop.user.common.model.UserScoreDetail;
import com.dlc.shop.user.common.model.UserScoreLog;
import com.dlc.shop.user.common.service.UserLevelService;
import com.dlc.shop.user.common.service.UserScoreDetailService;
import com.dlc.shop.user.common.service.UserScoreLogService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 用户等级提升操作
 *
 * @author lhd
 */
@Component("levelUpListener")
@AllArgsConstructor
public class LevelUpListener {

    private final UserLevelService userLevelService;
    private final SysConfigService sysConfigService;
    private final UserScoreDetailService userScoreDetailService;
    private final UserScoreLogService userScoreLogService;
    private final UserExtensionMapper userExtensionMapper;


    /**
     * 用户等级提升操作
     */
    @EventListener(LevelUpEvent.class)
    @Transactional(rollbackFor = Exception.class)
    public void levelUpListener(LevelUpEvent event) {
        List<UserExtension> userExtensions = event.getUserExtensions();
        List<UserScoreDetail> userScoreDetails = new ArrayList<>(userExtensions.size());
        List<UserScoreLog> userScoreLogs = new ArrayList<>(userExtensions.size());
        ScoreConfigParam scoreParam = sysConfigService.getSysConfigObject(Constant.SCORE_CONFIG, ScoreConfigParam.class);
        for (UserExtension userExtension : userExtensions) {
            if (Objects.equals(event.getIsNeedScore(), 1)) {
                if(scoreParam != null && scoreParam.getRegisterScore() != null) {
                    userExtension.setScore(scoreParam.getRegisterScore());
                    //添加积分明细
                    UserScoreDetail addDetail = new UserScoreDetail();
                    addDetail.setCreateTime(new Date());
                    addDetail.setStatus(1);
                    addDetail.setUserId(userExtension.getUserId());
                    addDetail.setUsableScore(scoreParam.getRegisterScore());
                    userScoreDetails.add(addDetail);
                    //添加积分日志
                    UserScoreLog userScoreLog = new UserScoreLog();
                    userScoreLog.setUserId(userExtension.getUserId());
                    userScoreLog.setScore(scoreParam.getRegisterScore());
                    userScoreLog.setSource(ScoreLogType.REGISTER.value());
                    userScoreLog.setCreateTime(new Date());
                    userScoreLog.setIoType(1);
                    userScoreLogs.add(userScoreLog);
                }
            }
        }
        if (Objects.equals(event.getIsNeedScore(), 1)) {
            if(scoreParam != null && scoreParam.getRegisterScore() != null) {
                // 注册更新用户积分
                userExtensionMapper.batchUpdateScore(userExtensions);
            }
        }



        List<UserLevel> userLevelList = userLevelService.selectLevelAndCoupons(null, null, event.getShopId());

        userLevelService.batchLevelUp(userExtensions, userLevelList, userScoreDetails, userScoreLogs);
        // 批量插入
        DataBatchHandleUtil.batchSplitInsert(userScoreDetails, userScoreDetailService::saveBatch);
        DataBatchHandleUtil.batchSplitInsert(userScoreLogs, userScoreLogService::saveBatch);
    }


}
